﻿using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Joker.Model.DataTransfer;

namespace Joker.View.Editor {
    public partial class GoodsDataEdit : Form {
        public int GoodsID = -1;
        public GoodsDataEdit() {
            InitializeComponent();
        }

        private void GoodsDataEdit_Load(object sender, EventArgs e) {
            DataTable table = DBHelper.GetDataTable($"SELECT * FROM [Goods] WHERE ID = {GoodsID}");
            if (GoodsID != -1) {
                textBox1.Text = GoodsID.ToString();
                textBox2.Text = table.Rows[0][1].ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e) {
            Close();
        }

        private void button2_Click(object sender, EventArgs e) {
            if (GoodsID == -1) {
                if (DBHelper.ExecuteNonQuery($"insert [Goods] values('{textBox2.Text}')")) {
                    MessageBox.Show("新增成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                } else {
                    MessageBox.Show("新增失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            } else {
                if (DBHelper.ExecuteNonQuery($"update [Goods] set [Name] = '{textBox2.Text}' where [ID] = {GoodsID}")) {
                    MessageBox.Show("编辑成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                } else {
                    MessageBox.Show("编辑失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                Close();
            }
        }
    }
}
